package com.smart.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.smart.entity.SsWork;
import com.smart.util.Pageable;
import com.smart.entity.SysUser;
import javax.annotation.Resource;
import com.smart.vo.SsAttendanceVO;
import cn.hutool.core.util.StrUtil;

import com.smart.common.util.SysUserUtils;
import com.smart.entity.SsAttendance;
import com.smart.service.SsWorkService;
import com.smart.service.WebInspectionRecordService;
import com.smart.mapper.AttendanceMapper;
import com.smart.vo.web.WebSsAttendanceVO;
import com.smart.service.AttendanceService;
import com.smart.entity.SysInspectionRecord;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * @author lgb
 * @date 2020年6月10日 下午7:38:28
 * Copyright (C) 深圳工勘科技
 * @说明: 智慧工地serviceImpl
 **/
@Service
public class AttendanceServiceImpl extends BaseServiceImpl<SsAttendance,
		Long> implements AttendanceService {

	@Resource
	private AttendanceMapper aMapper;
	@Resource
	private SsWorkService ssWorkService;
	@Resource
	private WebInspectionRecordService webInspectionRecordService;
	
	/**
	 * @param pageable
	 * @see 分页查询数据
	 **/
	@Override
	@Transactional(readOnly=true)
	public com.smart.util.Page<SsAttendance> findPage(Pageable pageable) {
		List<SsAttendance> list = new ArrayList<SsAttendance>();
		String name = pageable.getSearch().get("name");
		String identity = pageable.getSearch().get("identity");
		String startTime = pageable.getSearch().get("createDate_begin");
		String endTime = pageable.getSearch().get("createDate_end");
		int total = aMapper.findAttendanceTotal(name,identity,startTime,endTime);
		if(total == 0){
			return new com.smart.util.Page<SsAttendance>(list,pageable,Long.valueOf(total));
		}
		int start = (pageable.getPage()-1) * pageable.getLimit();
		int end = pageable.getPage() * pageable.getLimit();
		list = aMapper.findAttendanceList(name,identity,startTime,endTime,start,end);
		return new com.smart.util.Page<SsAttendance>(list,pageable,Long.valueOf(total));
	}

	/** 
	 * (non-Javadoc)
	 * @see: 
	 **/
	@Override
	@Transactional(readOnly=true)
	public SsAttendanceVO getLatestSsAttendance() {
		SysUser user = SysUserUtils.getSysUser();
		return aMapper.getLatestSsAttendance(user.getId());
	}

	/** 
	 * (non-Javadoc)
	 * @see: 查询页面
	 **/
	@Override
	@SuppressWarnings("unchecked")
	@Transactional(readOnly=true)
	public List<SsAttendance> findPage(String createTime,Integer pageNum, 
			Integer pageSize) {
		Long id = SysUserUtils.getSysUser().getId();
        LambdaQueryWrapper<SsAttendance> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SsAttendance::getUserId,id);
        if (StrUtil.isNotEmpty(createTime)) {
            wrapper.gt(SsAttendance::getCreateTime,createTime+" 00:00:00");
            wrapper.le(SsAttendance::getCreateTime,createTime+" 23:59:59");
        }
        wrapper.orderByDesc(SsAttendance::getCreateTime);
        IPage<SsAttendance> i=page(new Page<SsAttendance>(pageNum,pageSize),wrapper);
        return i.getRecords();
	}

	@Override
	public WebSsAttendanceVO findAttendanceList() {
		return null;
	}

	
	
}